﻿/*

	Eventually (hopefully) will be a handy little drawing Class.  It just draws the simple shapes,
	colors them, fills them, whatever.  Eventually I hope to have the basic shapes (circle, box,
	triangle, polygon(n) all of them with coloring and blending options.  We'll see though. . .

	Jason Crist
	5/23/05
	
	edited by Jeff Barker (added roundedBox)
	1/17/06

*/
class com.pbking.drawing.DrawShape{

	private function DrawShape(){
		//nothing here
	}

	//draw and colorfill a box (ref, x, y, width, height, color)
	public static function box(ref:MovieClip, x:Number, y:Number, w:Number, h:Number, color:Number, alfa:Number){
		roundedBox(ref, 0, x, y, w, h, 0, color, color, 0, alfa, true);
	}
	
	//draw an empty box with just an outline
	public static function outlineBox(ref:MovieClip, x:Number, y:Number, w:Number, h:Number, lw:Number, color:Number, alfa:Number){
		roundedBox(ref, 0, x, y, w, h, lw, color, color, alfa, alfa, false);
	}
	
	//draw an empty rounded box with just an outline
	public static function roundedOutlineBox(ref:MovieClip, c:Number, x:Number, y:Number, w:Number, h:Number, lw:Number, color:Number, alfa:Number){
		roundedBox(ref, c, x, y, w, h, lw, color, color, alfa, alfa, false);
	}
	
	//draw a rounded box with a fill and an outline
	public static function roundedBox(clip:MovieClip, c:Number, x:Number, y:Number, w:Number, h:Number, lw:Number, lcol:Number, fcol:Number, lalph:Number, falph:Number, fill:Boolean) {
		// if fill is not passed, it is true
		if (fill == null || fill == undefined) {
			fill = true;
		}
		// If fill color not passed, set it to white and clear
		if (fcol == null || fcol == null) {
			fcol = 0xFFFFFF;
			falph = 0;
		}
		// If other parameters not passed, set them to some default
		if (falph == null || falph == undefined) {
			falph = 100;
		}
		if (lw == null || lw == undefined) {
			lw = 1;
		}
		if (lcol == null || lcol == undefined) {
			lcol = 0x000000;
		}
		if (lalph == null || lalph == undefined) {
			lalph = 100;
		}

		// Adjust line width and height for curves
		if (c > 0) {
			w -= c * 2;
			h -= c * 2;
		}

		// Draw the box
		if (fill) {
			clip.beginFill(Number(fcol), Number(falph));
		}
		clip.lineStyle(lw, lcol, lalph);
		clip.moveTo(c + x, y);
		clip.lineTo(c + w + x, y);
		clip.curveTo((2 * c) + w + x, y, (2 * c) + w + x, c + y);
		clip.lineTo((2 * c) + w + x, c + h + y);
		clip.curveTo((2 * c) + w + x, (2 * c) + h + y, c + w + x, (2 * c) + h + y);
		clip.lineTo(c + x, (2 * c) + h + y);
		clip.curveTo(x, (2 * c) + h + y, x, c + h + y);
		clip.lineTo(x, c + y);
		clip.curveTo(x, y, c + x, y);
		if (fill) {
			clip.endFill();
		}
	}
	
	//draw a hollow box with an X through the middle of it.
	public static function xBox(ref:MovieClip, x:Number, y:Number, w:Number, h:Number, color:Number){
		ref.lineStyle(2, color, 100);
		ref.moveTo(x, y);
		ref.lineTo(x+w, y);
		ref.lineTo(x+w, y+h);
		ref.lineTo(x, y+h);
		ref.lineTo(x, y);
		ref.lineTo(x+w, y+h);
		ref.lineTo(x+w, y);
		ref.lineTo(x, y+h);
	}

	//draw an empty box
	public static function emptyBox(ref:MovieClip, x:Number, y:Number, w:Number, h:Number, color:Number){
		roundedBox(ref, 0, x, y, w, h, 2, color, color, 100, 100, false);
	}

	//fill an area with a graphic (tile);
	public static function tile(target:MovieClip, x:Number, y:Number, w:Number, h:Number, graphic:String){
		var fill:MovieClip = target.createEmptyMovieClip("fill", 0);
		fill.attachMovie(graphic, "s_0_0", 0);
		for(var i=0; i<w/fill.s_0_0._width; i++){
			for(var j=0; j<h/fill.s_0_0._height; j++){
				fill.attachMovie(graphic, "s_" + x + "_" + y, fill.getNextHighestDepth(), {_x:i*fill.s_0_0._width, _y:j*fill.s_0_0._height});
			}
		}
		var mask:MovieClip = target.createEmptyMovieClip("mask", 1);
		DrawShape.box(mask, x, y, w, h, 0);
		fill.setMask(mask);

	}
}